package com.iceph0x.android;

import java.util.List;

import com.iceph0x.android.aidl.IMyService;
import com.iceph0x.android.aidl.Person;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class ActivityOne extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        
        Button launchService = new Button(this);
        launchService.setId("service".hashCode());
        launchService.setText("bind service");
        launchService.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                bindPublicService();
            }
        });
        layout.addView(launchService);
        
        Button addPerson = new Button(this);
        addPerson.setText("add person");
        addPerson.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                addPerson();
            }
        });
        
        Button addPerson2 = new Button(this);
        addPerson2.setText("add person2");
        addPerson2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        addPerson();
                    }
                }).start();
            }
        });
        
        Button getAllPerson = new Button(this);
        getAllPerson.setText("get person");
        getAllPerson.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                getPersons();
            }
        });
        
        Button getAllPerson2 = new Button(this);
        getAllPerson2.setText("get person2");
        getAllPerson2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        getPersons();
                    }
                }).start();
            }
        });
        
        Button handlePerson = new Button(this);
        handlePerson.setText("handle person");
        handlePerson.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                handlePerson();
            }
        });
        layout.addView(addPerson);
        layout.addView(addPerson2);
        layout.addView(getAllPerson);
        layout.addView(getAllPerson2);
        layout.addView(handlePerson);
        
        setContentView(layout);
    }
    
    private IMyService mRemoteService;
    private boolean hasBindService;
    private ServiceConnection mRemoteConnecton = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mRemoteService = null;
        }
        
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mRemoteService = IMyService.Stub.asInterface(service);
        }
    };
    
    private void bindPublicService() {
        Button v = (Button) findViewById("service".hashCode());
        if (hasBindService) {
            unbindService(mRemoteConnecton);
            v.setText("bind service");
        }
        else {
            bindService(new Intent("com.iceph0x.android.aidl.IMyService"), mRemoteConnecton, Context.BIND_AUTO_CREATE);
            v.setText("unbind service");
        }
        
        hasBindService = !hasBindService;
    }
    
    private void addPerson() {
        if (!checkConnection())
            return;
        
        try {
            mRemoteService.addPerson(Person.born());
        }
        catch (RemoteException e) {
            e.printStackTrace();
        }
    }
    
    private void getPersons() {
        if (!checkConnection())
            return;
        
        try {
            List<Person> list = mRemoteService.getAllPerson();
            String tip = "has " + list.size() + " persons.\n";
            tip += "their are:";
            for (int i = 0; i < list.size(); ++i) {
                tip += "\n" + list.get(i);
            }
            Toast.makeText(this, tip, Toast.LENGTH_LONG).show();
        }
        catch (RemoteException e) {
            e.printStackTrace();
        }
    }
    
    private void handlePerson() {
        if (!checkConnection())
            return;
        
        try {
            List<Person> list = mRemoteService.getAllPerson();
            String tip;
            if (list.size() > 0) {
                tip = "handle person.";
                mRemoteService.getDataHandler().handle(list.get(list.size() - 1));
            }
            else {
                tip = "no person to handle.";
            }
            Toast.makeText(this, tip, Toast.LENGTH_LONG).show();
        }
        catch (RemoteException e) {
            e.printStackTrace();
        }
    }
    
    private boolean checkConnection() {
        StackTraceElement[] trace = Thread.currentThread().getStackTrace();
        System.out.println("activity::" + Process.myPid() + "," + Thread.currentThread().getId() + "@check by "
                + trace[1]);
        return mRemoteService != null;
    }
}